<?php
require_once 'modules/product/models/Product.php';
require_once 'modules/product/models/Input.php';
require_once 'modules/import/Excel/reader.php';

class import_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		$objCat = new Models_ProductCategory();
		$objProduct = new Models_Product();
		$data = new Spreadsheet_Excel_Reader();
		
		$data->read('modules/import/data.xls');
		$data = $data->sheets[0]['cells'];
//		echo '<pre>';print_r($data);die;
		/**
		 * Remove first row
		 */
		unset ($data[1]);
		/**
		 * Empty category level 2
		 */
		$objCat->delete('parent_id IS NOT NULL');
		/**
		 * Insert/Update data
		 */
		$cats1 = array();
		$cats2 = array();
		foreach ($data as $row) {
			$cat1Name = trim($row[1]);
			$cat2Name = trim($row[2]);
			/**
			 * Check cat1
			 */
			if (! isset($cats1[$cat1Name])) {
				/**
				 * Load from DB
				 */
				$tmp = current($objCat->getByColumns(array('name=?' => $cat1Name))->toArray());
				
				if (false == $tmp) {
					/**
					 * Not exsting
					 */
					$newData = array(
						'lang_id' => 1,
						'name' => $cat1Name,
						'alias' => $this->_slug($cat1Name),
						'created_date' => time(),
						'product_deleteable' => 1,
						'images' => "media/userfiles/images/category/{$this->_slug($cat1Name)}.png",
						'template' => 'default'
					);
					if (! is_file($newData['images'])) {
					    /**
					     * Copy default image
					     */
					    copy ('modules/import/category_noimage.png', $newData['images']);
					}
					$id = $objCat->insert($newData);
					$objCat->update(array('gid_string' => $id), array ('product_category_id=?' => $id));
					/**
					 * Update cat1
					 */
					$cats1[$cat1Name] = $id;
				} else {
					$cats1[$cat1Name] = $tmp['product_category_id'];
				}
			}
			/**
			 * Check cat2
			 */
			if (! isset($cats2[$cat2Name])) {
				/**
				 * Load from DB
				 */
				$tmp = current($objCat->getByColumns(array('name=?' => $cat2Name, 'parent_id=?' => $cats1[$cat1Name]))->toArray());
				
				if (false == $tmp) {
					/**
					 * Not exsting
					 */
					$newData = array(
						'lang_id' => 1,
						'parent_id' => $cats1[$cat1Name],
						'name' => $cat2Name,
						'alias' => $this->_slug($cat2Name),
						'created_date' => time(),
						'product_deleteable' => 1,
						'images' => "media/userfiles/images/category/{$this->_slug($cat1Name)}.png",
//						'images' => "media/userfiles/images/category/default.png",
						'template' => 'default'
					);
					if (! is_file($newData['images'])) {
					    /**
					     * Copy default image
					     */
					    copy ('modules/import/category_noimage.png', $newData['images']);
					}
					$id = $objCat->insert($newData);
					$objCat->update(array('gid_string' => $id), array ('product_category_id=?' => $id));
					/**
					 * Update parent gid_string
					 */
					$objCat->updateGidString($cats1[$cat1Name], $id);
					/**
					 * Update cat2
					 */
					$cats2[$cat2Name] = $id;
				} else {
					$cats2[$cat2Name] = $tmp['product_category_id'];
				}
			}
			/**
			 * Insert product
			 */
			$newData = array(
				'lang_id' => 1,
				'product_category_gid' => $cats2[$cat2Name],
				'sku' => $row[3],
				'title' => utf8_encode($row[4]),
				'alias' => $this->_slug($row[4]),
				'intro_text' => $row[5],
				'created_date' => time(),
				'images' => "media/userfiles/images/product/{$this->_slug($row[3])}.jpg",
//				'images' => "media/userfiles/images/product/default.jpg",
				'price' => floatval(str_replace('$', null, $row[6])),
			);
			if (! is_file($newData['images'])) {
			    /**
			     * Copy default image
			     */
			    copy ('modules/import/product_noimage.jpg', $newData['images']);
			}
			$id = $objProduct->insert($newData);
			
		}
		
//		echo "<pre>";print_r($data);die;
		echo "FINISHED :)";die;
	}
	
	private function _slug($str)
	{
		$str = strtolower(trim($str));
		$str = preg_replace('/[^a-z0-9-]/', '-', $str);
		$str = preg_replace('/-+/', "-", $str);
		/**
		 * Replace for image name from SKU
		 */
		$str = str_replace('/', '-', $str);
		return $str;
	}
}